package com.it.gw.modules.user.activity

import android.widget.Toast
import com.it.common.utils.DisplayUtil
import com.it.common.utils.QrCodeUtils
import com.it.common.utils.isNotEmpty
import com.it.data.manager.AppManager
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityQCodeBinding
import com.yili.pda.dialog.UpdateDialog

class QcodActivity : BaseActivity<ActivityQCodeBinding>() {
    override fun initObserver() {
        mainViewModel.versionLiveData.observe(this) {
            if (it.result && it.data.isNotEmpty()) {
                val bitmap =
                    QrCodeUtils.generateQrCode(it.data!!.URL, DisplayUtil.dip2px(this, 220f))
                binding.ivQCod.setImageBitmap(bitmap)


                if (it.data!!.VERSION > AppManager.getAppVersionName(this)) {

                    binding.tvVersion.text = "版本信息：${it.data?.VERSION}"
                    UpdateDialog.showUpdateDialog(
                        url = it.data!!.URL,
                        versionName = it.data!!.VERSION,
                        context = this
                    )
                }
                else{
                    binding.tvVersion.text = "当前已是最新版本"

                }



            } else {
                Toast.makeText(this, it.msg, Toast.LENGTH_LONG).show()
            }
        }


    }

    override fun initListener() {

    }

    override fun initData() {
        mainViewModel.version()

    }

    override fun onResume() {
        super.onResume()

    }

    override fun initView() {
        setTitle(
            title = R.string.str_version,
            isBack = true

        )

    }

    override fun setContentView() {
        binding = ActivityQCodeBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}